﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using rbac.Share;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace rbac.Services
{
    public class ServiceBase
    {
        protected readonly IMapper _mapper;
        protected readonly IHttpContextAccessor _contextAccessor;
        protected int LoginUserId;
        protected ServiceBase()
        {
            //从容器中获取服务实例
            _mapper=ServiceProviderLocator.instance.GetService<IMapper>()!;
            _contextAccessor = ServiceProviderLocator.instance.GetService<IHttpContextAccessor>()!;
            //判断一下是否授权了
            if(_contextAccessor.HttpContext!=null)
            {
                if(_contextAccessor.HttpContext.User.Identity!.IsAuthenticated)
                {
                    LoginUserId = Convert.ToInt32(_contextAccessor.HttpContext!.User.Claims.FirstOrDefault(p => p.Type.Equals("UserId")).Value);
                }
            }
           
        }

     }
}
